<?php
     include_once('../src/classes/class.mssql.php');  
     
     session_start();
     
     $db = new MSSQL();

     $comtec_id_order = $_GET['comtec_id_order'];
     $type            = $_GET['type'];
     $token           = $_GET['token'];
     
     $tsql = "SELECT m.cod_mainteiner,o.comtec_id_machine,o.comtec_id_responsible,
                     o.notes,o.operation_type,o.cod_order,o.tasks,o.comtec_id_mainteiner,
                     DATEPART(day,o.order_start_date) as op_day1,                     
                     DATEPART(month,o.order_start_date) as op_month1,
                     DATEPART(yyyy,o.order_start_date) as op_year1,                         
                     DATEPART(day,o.order_end_date) as op_day2,                     
                     DATEPART(month,o.order_end_date) as op_month2,
                     DATEPART(yyyy,o.order_end_date) as op_year2                     
              FROM   [dbo].[orders] AS o INNER JOIN [dbo].[machines] AS m ON o.comtec_id_machine = m.comtec_id_machine
              WHERE  o.comtec_id_order = $comtec_id_order";
     
     $result = $db->fetch_array($db->query($tsql));
     
     $comtec_id_mainteiner = $result['comtec_id_mainteiner'];
     $comtec_id_responsible = $result['comtec_id_responsible'];
     $comtec_id_machine = $result['comtec_id_machine'];
     $operation_type = $result['operation_type'];    
     $cod_mainteiner = $result['cod_mainteiner'];
     $cod_order = $result['cod_order'];
     $tasks = $result['tasks'];
     $notes = $result['notes'];
     $op_day1 = $result['op_day1'];
     $op_month1 = $result['op_month1'];
     $op_year1 = $result['op_year1'];
     $op_day2 = $result['op_day2'];
     $op_month2 = $result['op_month2'];
     $op_year2 = $result['op_year2'];
   
     function extra($x){
      if ($x <= 9){
       $x = "0".$x;   
      }   
      return $x;   
     }
?>    
<fieldset class="curved">
 <legend>Editar</legend>
 <table style="margin: 10px auto; border-spacing: 10px;">
        <tr><td width="200"><label>M&aacute;quina</label></td>
            <td><input type="text" DISABLED size="40" value="<?=$cod_mainteiner;?>" /></td>
            <td width="200"><label>C&oacute;digo</label></td>
            <td><input type="text" size="40" id="cod_order" value="<?=$cod_order;?>" /></td>
        </tr>
        <tr>
            <td width="200"><label>Tipo de operaci&oacute;n</label></td>
            <td>
                <select id="operation_type" onchange="javascript:operation_type()" style="width:257px;">
                       <?if($operation_type==0){?>
                       <option value="0" selected>Selecci&oacute;n...</option>
                       <?}else{?><option value="0">Selecci&oacute;n...</option><?}?>
                       <?if($operation_type==1){?>
                       <option value="1" selected>Mantenci&oacute;n</option>
                       <?}else{?><option value="1">Mantenci&oacute;n</option><?}?>
                       <?if($operation_type==2){?>
                       <option value="2" selected>Emergencia</option>
                       <?}else{?><option value="2">Emergencia</option><?}?>
                       <?if($operation_type==3){?>
                       <option value="3" selected>Instalaci&oacute;n</option>
                       <?}else{?><option value="3">Instalaci&oacute;n</option><?}?>
                       <?if($operation_type==4){?>
                       <option value="4" selected>Reparaci&oacute;n</option>
                       <?}else{?><option value="4">Reparaci&oacute;n</option><?}?>
                </select>
           </td>
           <td width="200"><label>Encargado<label></td>
           <td>
               <select id="comtec_id_responsible" style="width:257px;">
                    <option value="0" selected>Selecci&oacute;n...</option>
                    <?
                        $responsible = $db->query("SELECT comtec_id_responsible, name, lastname1, lastname2, status_reg FROM responsible WHERE comtec_id_mainteiner = $comtec_id_mainteiner ");
                        while ($r = sqlsrv_fetch_array($responsible,SQLSRV_FETCH_ASSOC))
                        {
                            if ($r['status_reg'] == 1)
                            {
                             if ($comtec_id_responsible==$r['comtec_id_responsible']){
                             echo "<option value=".$r['comtec_id_responsible']." selected>".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";
                             }else {
                              echo "<option value=".$r['comtec_id_responsible'].">".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";    
                             }
                             
                            }
                        }
                    ?>
               </select>
            </td>
           </tr>
           <tr>
               <td width="200"><label>Tareas</label></td>
               <td colspan=4><textarea id="tasks" rows="6" cols="91" style="resize:none"><?=$tasks?></textarea></td>
           </tr>
           <tr>
                <td width="200"><label>Notas</label></td>
                <td colspan=4><textarea id="notes" rows="2" cols="91" style="resize:none"><?=$notes?></textarea></td>
           </tr>
           <tr>              
               <td width="200"><label>Fecha de inicio</label></td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day1">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day1 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month1">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month1 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year1">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year1 == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
           </tr>   
           <tr>
               <td width="200">
                   <label>Fecha de fin</label>
               </td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day2" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day2 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month2" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month2 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year2" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year2 == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
           </tr>  
           <tr>
               <td><input type="button" value="Guardar" onclick="javascript:editOrder(<?=$comtec_id_order?>);" />
               <? if($type == 1){    
               echo '<input type="button" value="Cancelar" onclick="javascript:loadOrders('.$comtec_id_machine.');" /></td>';
               }?>
               <? if($type == 2){
               echo '<input type="button" value="Cancelar" onclick="javascript:loadHome();" /></td>';   
               }?>
               <? if($type == 3){
               echo '<input type="button" value="Cancelar" onclick="javascript:goBackSearch();" /></td>';    
               }?>
           </tr>
          </table>
 
  <input type="hidden" id="comtec_id_machine" value="<?=$comtec_id_machine?>" />
  <input type="hidden" id="comtec_id_mainteiner" value="<?=$comtec_id_mainteiner?>" />
  <input type="hidden" id="type_order" value="<?=$type?>" />
  
  <div id="result"></div> 
</fieldset>
